package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/set/{key}/{value}")
    public String setKey(@PathVariable String key, @PathVariable String value) {
        System.out.println("key:" + key + ",value:" + value);
        redisTemplate.opsForValue().set(key, value,120);
        return "Key set successfully!";
    }

    @GetMapping("/get/{key}")
    public String getKey(@PathVariable String key) {
        System.out.println("查询的key值：" + key);
        Object value = redisTemplate.opsForValue().get(key);
        return value != null ? value.toString() : "Key not found!";
    }
}
